<?php
namespace app\web\home;


use app\web\model\Coupon as myModel;
use app\web\model\CouponType;
use app\web\model\Technician;
use app\web\model\Project;
use app\web\model\Shop;

/**
 * 首页\公共接口
 */
class Coupon extends Base 
{
    /**
     * 下单页面优惠券弹窗
     * [getCouponByProjectID description]
     * @param  [type] $project_id [description]
     * @return [type]             [description]
     */
    public function getCouponByProjectID($project_id)
    {
        $project = Project::get($project_id);

        if($project['shop_id'] > 0){   //商家项目
            $list = myModel::where([['user_id','=',$this->uid],['status','=',0],['shop_id','=',$project['shop_id']]])
                        ->field('id,type_id,type_name,money,full_money')
                        ->order('id desc')
                        ->select()->toArray();
        }else{   //技师项目
            $list = myModel::where([['user_id','=',$this->uid],['status','=',0],['technician_id','=',$project['technician_id']]])
                        ->field('id,type_id,type_name,money,full_money')
                        ->order('id desc')
                        ->select()->toArray();
        }
        $array = myModel::where([['user_id','=',$this->uid],['status','=',0],['coupon_type','=',0]])
                        ->field('id,type_id,type_name,money,full_money')
                        ->order('id desc')
                        ->select()->toArray();

        $ids = myModel::where(['user_id'=>$this->uid])->column('type_id');
        $ids = array_unique($ids);
        $where[] = ['status','=',1];
        $where[] = ['check_status','=',1];
        if($ids) $where[] = ['id','not in',$ids];
        $where[] = ['user_id','<>',$this->uid];
  
        $coupon_type = CouponType::where($where)->field('0 as id,id as type_id,type_name,money,full_money')->select()->toArray();
   
        $array = array_merge($list,$array,$coupon_type);
        return show(200,'优惠券',$array);
    }
    /**
     * 技师端商家端我添加的优惠券列表
     * [getCouponType description]
     * @return [type] [description]
     */
    public function getCouponType()
    {
        if($this->appkey == 0) return show(200,'无权限查看',[]);
        $id = $this->getCurrentId();
        if($this->appkey == 1) {
            $where= ['technician_id' => $id];
        }else{
            $where= ['shop_id' => $id];
        }
        $list = CouponType::where($where)->field('id,type_name,money,full_money,status')->order('id desc')->all();
        return show(200,'优惠券类别列表',$list);
    }
    /**
     * 添加优惠券
     * [addType description]
     */
    public function addType()
    {
        if($this->auth_status == 2) return show(400,'无操作权限');
        if($this->appkey == 0) return show(400,'无权限添加优惠券');
        $post = input('post.');
        $id = $this->getCurrentId();
        if($this->appkey == 1) {   //技师
            $user = Technician::get($id);
            if($user->shop_id > 0) return show(400,'已绑定商户，不可添加优惠券');
            $post['coupon_type'] = 2;
            $post['technician_id'] = $id;
            $post['type_name'] = $user['nickName'];
        }else{
            $shop = Shop::get($id);
            $post['coupon_type'] = 1;
            $post['shop_id'] = $id;
            $post['type_name'] = $shop['shop_name'];
        }
        $post['status'] = 0;
        $this->validate($post,'CouponType');
        $post['user_id'] = $this->uid;
        if(bccomp($post['money'], $post['full_money'],2) != -1){
            return show(200,'面值必须小于满N元可用');
        }
        $res = CouponType::create($post);
        if($res->isEmpty()) return show(400,'添加失败');
        return show(200,'添加成功');
    }
    /**
     * 修改类别
     * [editCouponTyp description]
     * @return [type] [description]
     */
    public function editCouponTyp()
    {
        if($this->auth_status == 2) return show(400,'无操作权限');
        $post = input('post.');
        $info = CouponType::get($post['id']);
        if($info->save($post)){
            return show(200,'修改成功');
        }
        return show(400,'修改失败');
    }
    /**
     * 删除优惠券类别
     * [delCouponType description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delCouponType($id)
    {
        if($this->auth_status == 2) return show(400,'无操作权限');
        CouponType::destroy($id);
        return show(200,'删除成功');
    }
    /**
     * 上架 下架
     * [disCouponType description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function disCouponType($id)
    {
        if($this->auth_status == 2) return show(400,'无操作权限');
        $info = CouponType::get($id);
        $info->status = $info->status == 1 ? 0 : 1;
        if($info->status == 1){
            if($info->check_status == 0) return show(400,'等待商家审核');
            if($info->check_status == 2) return show(400,'审核未通过');
        }
        $info->save();
        return show(200,'操作成功');
    }
    /**
     * 可领取优惠券
     * [getList description]
     * @param  integer $coupon_type [description]
     * @return [type]               [description]
     */
    public function getList($coupon_type = 0)
    {
        $ids = myModel::where(['user_id'=>$this->uid])->column('type_id');
        $ids = array_unique($ids);
        $where[] = ['status','=',1];
        $where[] = ['check_status','=',1];
        if($ids) $where[] = ['id','not in',$ids];
        
        if($coupon_type == 0){
            $where[] = ['coupon_type','=',0];
        }else{
            $where[] = ['user_id','<>',$this->uid];
            $where[] = ['coupon_type','>',0];
        }
        
        $list = CouponType::where($where)->field('id,type_name,money,full_money')->paginate();
        return show(200,'优惠券',$list);
    }
    /**
     * 领取卡券
     * [receive description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function receive($id)
    {
        $coupon = myModel::where(['type_id'=>$id,'user_id'=>$this->uid])->find();
        if($coupon) return show(400,'不可重复领取');
        $coupon_type = CouponType::get($id);
        $res = myModel::create([
            'user_id' => $this->uid,
            'shop_id' => $coupon_type->shop_id,
            'technician_id' => $coupon_type->technician_id,
            'type_id' => $coupon_type->id,
            'type_name' => $coupon_type->type_name,
            'money' => $coupon_type->money,
            'full_money' => $coupon_type->full_money,
        ]);
        if($res->isEmpty()) return show(400,'领取失败');
        return show(200,'领取成功',$res->id);
    }




    //+++++++++++++++++++++++用户端
    /**
     * 我的优惠券列表
     * [myList description]
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    public function myList($status = 0)
    {
        $list = myModel::where(['user_id'=>$this->uid,'status'=>$status])
                        ->field('id,type_name,status,money,full_money')
                        ->order('id desc')
                        ->paginate();
        return show(200,'我的优惠券',$list);
    }
}